// --------------------------------------------------------------------
//   name 	:  Color Ranking Page
//   desc 	:  Shows you which tribes are your enemies/nap/allies on the rankings page
//   version 	: 0.1
//   dev 	: JelianSD
// --------------------------------------------------------------------
// DEPENDENCIES
// 
// --------------------------------------------------------------------
// CHANGE HISTORY
// 0.1 	Initial Release
// --------------------------------------------------------------------
plugins.colorRankingsPages = {
	'colorthemrankings' : function(userTribeInfo) {
		colors = {
			'enemy' : getValuePlugin("CRP_EnemyColor"),
			'ally': getValuePlugin("CRP_AlliesColor"),
			'nap': getValuePlugin("CRP_NapColor"),
			'ownTribe': getValuePlugin("CRP_TribeColor"),
			'self': getValuePlugin("CRP_SelfColor")
		};
		for (i in userTribeInfo) {
			if (i != 'LastUpdated') {
				trb = userTribeInfo[i];
				$("table.vis tr a[href$=ally&id="+trb.id+"]").parent().parent().css({"border":"medium double #"+colors[trb.type],"border-left":"12px solid #"+colors[trb.type]});
			};
		};
		$(".lit").css({"border":"medium double #"+colors['self'],"border-left":"12px solid #"+colors['self']});
		$(".lit2").css({"border":"medium double #"+colors['ownTribe'],"border-left":"12px solid #"+colors['ownTribe']});
	},
	'enhance_game_ranking' : function() {
		time = parseInt(new Date().getTime().toString().substring(0, 10));
		if(!getValue("TribeInfo") || (getValue("TribeInfo").LastUpdated+21600)<time) {
			pUrl = twe.Server+'/game.php?screen=ally&mode=contracts';
			pData = '';
			jQuery.get( pUrl, pData, function(xhr) {
				userTribeInfo = {};
				TypeOfTribe = ["ally","nap","enemy"];
				if(getValue("TribeInfo")) userTribeInfo=getValue("TribeInfo");
				userTribeInfo.LastUpdated=time;
				count = 0;
				$("table.vis:gt(0)", xhr).each(function() {
					if(count>2) return;
					$("a[href*=info_ally]", this).each(function() {
						tmp = {};
						tmp.id = $(this).attr("href").replace(/(.*)id=(.+)$/i, "$2");
						tmp.tag = $(this).html();
						tmp.type = TypeOfTribe[count];
						
						userTribeInfo[tmp.id] = tmp;
					});
					count++;
				});
				setValue("TribeInfo",userTribeInfo);
				plugins.colorRankingsPages.colorthemrankings(getValue("TribeInfo"));
			});
		} else {
			plugins.colorRankingsPages.colorthemrankings(getValue("TribeInfo"));
		};
		
	},
	'info' : {
		'mandatory' : false,
		'name' : "Color Rankings Page",
		'desc' : "Shows which tribes are your enemies/allies/nap with your tribe.",
		'version' : "0.1",
		'dev' : "JelianSD + Timas"
	},
	'variables' : {
		'desc':["Your tribes color","Your color","Allied tribes color","NAP tribes color","Enemy tribes color"],
		'CRP_TribeColor':"99FF99",
		'CRP_SelfColor':"FFFFFF",
		'CRP_AlliesColor':"66CCFF",
		'CRP_NapColor':"CC44FF",
		'CRP_EnemyColor':"FF0066"
	}
}